package com.jeeplus.modules.web.wx;

import com.jeeplus.common.config.Global;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.TimeUtils;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.web.entity.WxUserRoomQrcode;
import com.jeeplus.modules.web.entity.YjMahjongHall;
import com.jeeplus.modules.web.service.WxUserRoomQrcodeService;
import com.jeeplus.modules.web.service.YjMahjongHallService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author liangbing
 * @version 1.0
 * @date 2021/4/1 8:46
 * @description 微信用户-棋牌室房间二维关系controller
 */
@Controller
@RequestMapping(value = "wx/deprecated/userRoomQrcode")
@Deprecated
public class WxUserRoomQrcodeController extends BaseController {

    @Autowired
    private WxUserRoomQrcodeService wxUserRoomQrcodeService;

    @Autowired
    private YjMahjongHallService yjMahjongHallService;

    private static String roomQrcodeActivateHours = Global.getConfig("roomQrcodeActivateHours");// 房间二维码有效小时数

    /**
     * 新建用户-棋牌室房间二维码关系
     * @param wxUserRoomQrcode
     * @return
     */
    @ApiOperation(notes = "create", httpMethod = "POST", value = "新建用户-棋牌室房间二维码关系")
    @ResponseBody
    @PostMapping(value = "create")
    public AjaxJson create(@RequestBody @Validated WxUserRoomQrcode wxUserRoomQrcode){
        AjaxJson j = new AjaxJson();

        // 检查二维码是否有效
        YjMahjongHall yjMahjongHall = yjMahjongHallService.getByRoomQrcode(wxUserRoomQrcode.getRoomQrcode());
        if (yjMahjongHall == null) {
            j.setSuccess(false);
            j.setMsg("二维码不存在或者未绑定房间！");
            return j;
        }

        // 新建用户-房间二维码关系
        wxUserRoomQrcode.setLastActivateDate(new Date());
        wxUserRoomQrcodeService.insertOrUpdate(wxUserRoomQrcode);

        j.setSuccess(true);
        return j;
    }

    /**
     * 检查房间二维码是否有效
     * @param roomQrcode
     * @return
     */
    @ApiOperation(notes = "checkRoomQrcodeIsValid", httpMethod = "GET", value = "检查房间二维码是否有效")
    @ApiImplicitParams({@ApiImplicitParam(name = "roomQrcode", value = "房间二维码", required = true, paramType = "query", dataType = "string")})
    @ResponseBody
    @GetMapping(value = "checkRoomQrcodeIsValid")
    public AjaxJson checkRoomQrcodeIsValid(String roomQrcode) {
        AjaxJson j = new AjaxJson();

        // 检查二维码是否有效
        WxUserRoomQrcode record = wxUserRoomQrcodeService.getByRoomQrcode(roomQrcode);
        if (record == null) {
            j.setSuccess(false);
            j.setMsg("二维码不存在或者未绑定房间！");
            return j;
        }

        // 检查是否已经超时
        int activeHours = 8; //默认有效时间为8小时
        if (StringUtils.isNotBlank(roomQrcodeActivateHours)) {
            activeHours = Integer.parseInt(roomQrcodeActivateHours);
        }

        // 获得上次激活时间
        Date lastActivateDate = record.getLastActivateDate();
        int hours = TimeUtils.getHoursBetween(lastActivateDate, new Date());
        if (hours > activeHours) {
            j.setSuccess(false);
            j.setMsg("二维码已过期，请重新扫码！");
            return j;
        }

        j.setSuccess(true);
        return j;
    }
}
